package com.kent.hellokotlin.cClassAndObject

import org.jetbrains.annotations.Nullable

//属性与字段

//声明属性

class Address {
    var name: String = "Holmes, Sherlock"
    var street: String = "Baker"
    var city: String = "London"
    var state: String? = null
    var zip: String = "123456"
}

//Getters 与 Setters

class Getter {
    var size = 0

    val empty
        get():Boolean {
            return size == 0
        }
}

class Setter {
    var toString: String = "up"
        get() = "i am $field"
        set(value) {
            field = "setted to $value"
        }
}

fun fooGetSet() {
    val getter = Getter()
    println(getter.empty)
    getter.size = 1
    println(getter.empty)

    println("-------------------------------------------")

    val setter = Setter()
    println(setter.toString)
    setter.toString = setter.toString
    println(setter.toString)
}

//如果你需要改变一个访问器的可见性或者对其注解，但是不需要改变默认的实现， 你可以定义访问器而不定义其实现:
var setterVisibility: String = "abc"
    private set // 此 setter 是私有的并且有默认实现

var setterWithAnnotation: Any? = null
    @Nullable set // 用 Inject 注解此 setter

//幕后字段
var counter = 0 // 注意：这个初始器直接为幕后字段赋值
    set(value) {
        if (value >= 0) field = value
    }

//幕后属性
private var mTable: Map<String, Int>? = null
public val table: Map<String, Int>
    get() {
        if (mTable == null) {
            mTable = HashMap()
        }
        return mTable ?: throw AssertionError("Set to null by other thread.")
    }

//延迟初始化属性与变量
public class Test {
    lateinit var test:Any
    fun fooTest() {
        ::test.isInitialized
    }
}

fun main() {

}